<html>
<head>
<title>twovar.inc</title>
<style type="text/css">
<!--
pre{ font-family: "Courier New", Courier, monospace;
     background-color: #fafafa;
     border: 1px solid ; border-color: #000000;
     padding: 5pt;}

pre a:hover {background-color: #eeeeff;}

.med {color: #880088; font-weight: bold;}    /*Media*/
.func {color: #880088;}                      /*Functions (float, vector, string)*/
.mat {color: #880088; font-weight: bold;}    /*Materials*/
.squig {color: #0000dd;}                     /*{}*/
.cammod {color: #880088;}                    /*Camera modifiers*/
.glob {color: #880088; font-weight: bold;}   /*Global*/
.ident {font-weight: bold;}                  /*Identifiers*/
.obmod {color: #880088;}                     /*Object modifiers*/
.globmod {color: #880088;}                   /*Global modifiers*/
.dirc {color: #880088; font-weight: bold;}   /*Directives*/
.msg {color: #880088; font-weight: bold;}    /*Message streams*/
.atmo {color: #880088; font-weight: bold;}   /*Atmospheric objects*/
.matmod {color: #880088;}                    /*Material modifiers*/
.io {color: #880088; font-weight: bold;}     /*File I/O*/
.slcom {color: #009922; font-style: italic;} /*Single line comment*/
.obj {color: #880088; font-weight: bold;}    /*Objects*/
.mod {color: #880088;}                       /*Modifiers (general)*/
.inter {color: #880088; font-weight: bold;}  /*Interior*/
.atmod {color: #880088;}                     /*Atmospheric modifiers*/
.str {color: #ff0000;}                       /*Strings*/
.medmod {color: #880088;}                    /*Media modifiers*/
.num {color: #009999;}                       /*Numbers*/
.ptrn {color: #880088;}                      /*Patterns*/
.mlcom {color: #009922; font-style: italic;} /*Multi-line comment*/
.math {color: #ff0000;}                      /*Mathematical operators*/
.cam {color: #880088; font-weight: bold;}    /*Camera*/
.lite {color: #880088; font-weight: bold;}   /*Light source*/
.cond {color: #880088; font-weight: bold;}   /*Conditionals*/
.litmod {color: #880088;}                    /*Light modifiers*/
.dot {color: #880088;}                       /*Dot operators*/

-->
</style>
</head>
<body>
<p>twovar.inc :</p>
<pre><span class="slcom">// Persistence of Vision Ray Tracer Include File</span>
<span class="slcom">// File: twovarsurf.inc</span>
<span class="slcom">// Vers: 3.5</span>
<span class="slcom">// Desc: Generates a surface based on a function with two variables.</span>
<span class="slcom">// Date: 2001/04/27</span>
<span class="slcom">// Auth: Ingo Janssen</span>

<span class="slcom">// rev. 2002/22/10: For uv-mapping the texture is now taken from &lt;0,0&gt;-&lt;1,1&gt;,</span>
<span class="slcom">//                  this used to be &lt;UVmin&gt;-&lt;UVmax&gt;. So this may break some scenes!</span>

<span class="mlcom">/*======
TwoVarSurf(__Fuv, Urange, Vrange, Iter_U, Iter_V, FileName): Builds a mesh2 surface of
           a function with two variables (u,v).
__Fuv    : the function to be turned into a mesh2.
Urange   : A 2-D vector that gives the boundaries of u.
Vrange   : A 2-D vector that gives the boundaries of v. These are the ranges
           within whitch the surface is calculated. 
Iter_U   : Sets the resolution of the mesh in the u range.
Iter_V   : Sets the resolution of the mesh in the v range.
FileName : The name of the file to whitch the mesh will be written. If is an
           empty string (&quot;&quot;), no file will be written.
           If the file extension is 'obj' a Wavefront objectfile will be written.
           If the extension is 'pcm' a compressed mesh file is written.
           If a file name is given, the macro will first check if it already exists.
           If that is so, it will try to parse the existing file unless it's a '*.obj',
           '*.pcm' or '*.arr' file as POV-Ray can not read them directly. In this case a new
           mesh will be generated, but the existing files will _not_ be over-written.
Use:

   #include twovar.inc
   
   #declare __Fuv=function(u,v){(10*sin((u^2+v^3)^0.5))/(2+cos((u^2+y^2)^0.5))}
   
   object {
      TwoVarSurf(&lt;-6,6&gt;,&lt;-6,6&gt;,20,20,&quot;&quot;)
      uv_mapping
      pigment{checker color rgb &lt;0,0,0.2&gt; color rgb &lt;1,0.85,0.85&gt; scale 0.5}
      finish{specular 0.4}
      rotate &lt;-110,45,0&gt;
   }           
*/</span>

<span class="dirc">#version</span> <span class="num">3.5</span>;
<span class="dirc">#include</span> <span class="str">&quot;makemesh.inc&quot;</span>

<span class="dirc">#macro</span> <a name="TwoVarSurf"><span class="ident">TwoVarSurf</span></a>(__Fuv, Urange, Vrange, Iter_U, Iter_V, FileName)
   <span class="dirc">#declare</span> <a name="Build"><span class="ident">Build</span></a><span class="math">=</span>CheckFileName(FileName);
   <span class="cond">#if</span>(<a href="#Build">Build</a><span class="math">=</span><span class="num">0</span>)
      <span class="msg">#debug</span> <span class="func">concat</span>(<span class="str">&quot;\n Parsing mesh2 from file: &quot;</span>, FileName, <span class="str">&quot;\n&quot;</span>)
      <span class="dirc">#include</span> FileName
      <span class="obj">object</span><span class="squig">{</span>Surface<span class="squig">}</span>
   <span class="cond">#else</span>
      <span class="dirc">#local</span> Umin<span class="math">=</span>Urange<span class="num">.</span><span class="dot">u</span>;
      <span class="dirc">#local</span> Umax<span class="math">=</span>Urange<span class="num">.</span><span class="dot">v</span>;
      <span class="dirc">#local</span> Vmin<span class="math">=</span>Vrange<span class="num">.</span><span class="dot">u</span>;
      <span class="dirc">#local</span> Vmax<span class="math">=</span>Vrange<span class="num">.</span><span class="dot">v</span>;
      <span class="dirc">#local</span> dU<span class="math">=</span>Umax<span class="math">-</span>Umin;
      <span class="dirc">#local</span> dV<span class="math">=</span>Vmax<span class="math">-</span>Vmin;
      <span class="dirc">#local</span> iU<span class="math">=</span>dU<span class="math">/</span>Iter_U;
      <span class="dirc">#local</span> iV<span class="math">=</span>dV<span class="math">/</span>Iter_V;
      <span class="dirc">#local</span> NumVertices<span class="math">=</span>(Iter_U<span class="math">+</span><span class="num">1</span>)<span class="math">*</span>(Iter_V<span class="math">+</span><span class="num">1</span>);
      <span class="dirc">#declare</span> <a name="NumFaces"><span class="ident">NumFaces</span></a><span class="math">=</span>Iter_U<span class="math">*</span>Iter_V<span class="math">*</span><span class="num">2</span>;
      <span class="msg">#debug</span> <span class="func">concat</span>(<span class="str">&quot;\n Calculating &quot;</span>,<span class="func">str</span>(NumVertices,<span class="num">0</span>,<span class="num">0</span>),<span class="str">&quot; vertices for &quot;</span>, <span class="func">str</span>(<a href="#NumFaces">NumFaces</a>,<span class="num">0</span>,<span class="num">0</span>),<span class="str">&quot; triangles\n\n&quot;</span>)
      <span class="dirc">#local</span> VecArr<span class="math">=</span><span class="func">array</span>[NumVertices] 
      <span class="dirc">#local</span> NormArr<span class="math">=</span><span class="func">array</span>[NumVertices] 
      <span class="dirc">#local</span> UVArr<span class="math">=</span><span class="func">array</span>[NumVertices]
      <span class="dirc">#local</span> Count<span class="math">=</span><span class="num">0</span>;   
      <span class="dirc">#local</span> I<span class="math">=</span><span class="num">0</span>;  
      <span class="cond">#while</span> (I<span class="math">&lt;</span>Iter_V<span class="math">+</span><span class="num">1</span>)
         <span class="dirc">#local</span> V<span class="math">=</span>RangeMM(I,<span class="num">0</span>,Iter_V,Vmin,Vmax);
         <span class="dirc">#local</span> J<span class="math">=</span><span class="num">0</span>;
         <span class="cond">#while</span> (J<span class="math">&lt;</span>Iter_U<span class="math">+</span><span class="num">1</span>)
            <span class="dirc">#local</span> U<span class="math">=</span>RangeMM(J,<span class="num">0</span>,Iter_U,Umin,Umax);
            <span class="cond">#if</span>(J<span class="math">=</span><span class="num">0</span>)
               <span class="dirc">#local</span> P0<span class="math">=</span><span class="math">&lt;</span>U,V,__Fuv(U,V)<span class="math">&gt;</span>;
               <span class="dirc">#local</span> P2<span class="math">=</span><span class="math">&lt;</span>U<span class="math">-</span>iU,V,__Fuv(U<span class="math">-</span>iU,V)<span class="math">&gt;</span>;
            <span class="cond">#else</span>
               <span class="dirc">#local</span> P0<span class="math">=</span>P1;
               <span class="dirc">#local</span> P2<span class="math">=</span>P0;
            <span class="cond">#end</span>
            <span class="dirc">#local</span> P1<span class="math">=</span><span class="math">&lt;</span>U<span class="math">+</span>iU,V,__Fuv(U<span class="math">+</span>iU,V)<span class="math">&gt;</span>;
            <span class="dirc">#local</span> P3<span class="math">=</span><span class="math">&lt;</span>U,V<span class="math">+</span>iV,__Fuv(U,V<span class="math">+</span>iV)<span class="math">&gt;</span>;
            <span class="dirc">#local</span> P4<span class="math">=</span><span class="math">&lt;</span>U,V<span class="math">-</span>iV,__Fuv(U,V<span class="math">-</span>iV)<span class="math">&gt;</span>;
            <span class="dirc">#local</span> B1<span class="math">=</span>P4<span class="math">-</span>P0;
            <span class="dirc">#local</span> B2<span class="math">=</span>P2<span class="math">-</span>P0;
            <span class="dirc">#local</span> B3<span class="math">=</span>P3<span class="math">-</span>P0;
            <span class="dirc">#local</span> B4<span class="math">=</span>P1<span class="math">-</span>P0;
            <span class="dirc">#local</span> N1<span class="math">=</span><span class="func">vcross</span>(B1,B2);
            <span class="dirc">#local</span> N2<span class="math">=</span><span class="func">vcross</span>(B2,B3);
            <span class="dirc">#local</span> N3<span class="math">=</span><span class="func">vcross</span>(B3,B4);
            <span class="dirc">#local</span> N4<span class="math">=</span><span class="func">vcross</span>(B4,B1);
            <span class="dirc">#local</span> Norm<span class="math">=</span><span class="func">vnormalize</span>((N1<span class="math">+</span>N2<span class="math">+</span>N3<span class="math">+</span>N4));
            <span class="dirc">#local</span> VecArr[Count]<span class="math">=</span>P0;  
            <span class="dirc">#local</span> NormArr[Count]<span class="math">=</span>Norm;  
            <span class="dirc">#local</span> UVArr[Count]<span class="math">=</span><span class="math">&lt;</span>(U<span class="math">-</span>Umin)<span class="math">/</span>dU,(V<span class="math">-</span>Vmin)<span class="math">/</span>dV<span class="math">&gt;</span>;
            <span class="dirc">#local</span> Count<span class="math">=</span>Count<span class="math">+</span><span class="num">1</span>;
            <span class="dirc">#local</span> J<span class="math">=</span>J<span class="math">+</span><span class="num">1</span>;            
         <span class="cond">#end</span>
         <span class="msg">#debug</span> <span class="func">concat</span>(<span class="str">&quot;\r Done &quot;</span>, <span class="func">str</span>(Count,<span class="num">0</span>,<span class="num">0</span>),<span class="str">&quot; vertices : &quot;</span>,<span class="func">str</span>(<span class="num">100</span><span class="math">*</span>Count<span class="math">/</span>NumVertices,<span class="num">0</span>,<span class="num">2</span>),<span class="str">&quot; %&quot;</span>)
         <span class="dirc">#local</span> I<span class="math">=</span>I<span class="math">+</span><span class="num">1</span>;
      <span class="cond">#end</span>
      BuildWriteMesh2(VecArr, NormArr, UVArr, Iter_U, Iter_V, FileName)
   <span class="cond">#end</span>
<span class="cond">#end</span>

</pre>
</body>
</html>